#include "array_Queue.h"
#include <stdexcept>

template <class Object>
Queue<Object>::Queue(int capacity): theArray(capacity)
{
	makeEmpty();
}

template <class Object>
void Queue<Object>::makeEmpty()
{
	currentSize = 0;
	front = 0;
	back = -1;
}

template <class Object>
void Queue<Object>::enqueue(const Object &x)
{
	if (isFull())
		throw range_error("queue full");
	increment(back);
	theArray[back] = x;
	currentSize++;
}

template <class Object>
Object Queue<Object>::dequeue()
{
	if (isEmpty())
		throw range_error("queue empty");
	
	currentSize--;
	Object frontItem = theArray[front];
	increment(front);
	return frontItem;
}

template <class Object>
void Queue<Object>::increment(int &x)
{
	if (++x == theArray.size())
		x = 0;
}

template <class Object>
bool Queue<Object>::isEmpty() const
{
	return currentSize == 0;
}

template <class Object>
bool Queue<Object>::isFull() const
{
	return currentSize == (theArray.size() - 1);
}

template <class Object>
const Object & Queue<Object>::getFront() const
{
	if (isEmpty())
		throw range_error("queue empty");

	return theArray[front];	
}
